package com.castle.fortress.admin.oa.service.impl;

import cn.hutool.core.util.StrUtil;
import com.castle.fortress.admin.system.entity.SysDeptEntity;
import com.castle.fortress.admin.system.entity.SysUser;
import com.castle.fortress.admin.system.service.SysDeptService;
import com.castle.fortress.admin.utils.BizCommonUtil;
import com.castle.fortress.common.utils.ConvertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castle.fortress.admin.oa.entity.AttendanceSettingEntity;
import com.castle.fortress.admin.oa.dto.AttendanceSettingDto;
import com.castle.fortress.admin.oa.mapper.AttendanceSettingMapper;
import com.castle.fortress.admin.oa.service.AttendanceSettingService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.List;

/**
 * 考勤规则设置 服务实现类
 *
 * @author whc
 * @since 2022-11-03
 */
@Service
public class AttendanceSettingServiceImpl extends ServiceImpl<AttendanceSettingMapper, AttendanceSettingEntity> implements AttendanceSettingService {

    @Autowired
    SysDeptService sysDeptService;

    /**
     * 获取查询条件
     *
     * @param attendanceSettingDto
     * @return
     */
    public QueryWrapper<AttendanceSettingEntity> getWrapper(AttendanceSettingDto attendanceSettingDto) {
        QueryWrapper<AttendanceSettingEntity> wrapper = new QueryWrapper();
        if (attendanceSettingDto != null) {
            AttendanceSettingEntity attendanceSettingEntity = ConvertUtil.transformObj(attendanceSettingDto, AttendanceSettingEntity.class);
            wrapper.eq(attendanceSettingEntity.getType() != null, "type", attendanceSettingEntity.getType());
            wrapper.like(StrUtil.isNotEmpty(attendanceSettingEntity.getName()), "name", attendanceSettingEntity.getName());
            wrapper.orderByDesc("create_time");
        }
        return wrapper;
    }


    @Override
    public IPage<AttendanceSettingDto> pageAttendanceSetting(Page<AttendanceSettingDto> page, AttendanceSettingDto attendanceSettingDto) {
        QueryWrapper<AttendanceSettingEntity> wrapper = getWrapper(attendanceSettingDto);
        Page<AttendanceSettingEntity> pageEntity = new Page(page.getCurrent(), page.getSize());
        Page<AttendanceSettingEntity> attendanceSettingPage = baseMapper.selectPage(pageEntity, wrapper);
        Page<AttendanceSettingDto> pageDto = new Page(attendanceSettingPage.getCurrent(), attendanceSettingPage.getSize(), attendanceSettingPage.getTotal());
        pageDto.setRecords(ConvertUtil.transformObjList(attendanceSettingPage.getRecords(), AttendanceSettingDto.class));
        return pageDto;
    }

    @Override
    public IPage<AttendanceSettingDto> pageAttendanceSettingExtends(Page<AttendanceSettingDto> page, AttendanceSettingDto attendanceSettingDto) {
        Map<String, Long> pageMap = BizCommonUtil.getPageParam(page);
        AttendanceSettingEntity attendanceSettingEntity = ConvertUtil.transformObj(attendanceSettingDto, AttendanceSettingEntity.class);
        List<AttendanceSettingEntity> attendanceSettingList = baseMapper.extendsList(pageMap, attendanceSettingEntity);
        Long total = baseMapper.extendsCount(attendanceSettingEntity);
        Page<AttendanceSettingDto> pageDto = new Page(page.getCurrent(), page.getSize(), total);
        pageDto.setRecords(ConvertUtil.transformObjList(attendanceSettingList, AttendanceSettingDto.class));
        return pageDto;
    }

    @Override
    public AttendanceSettingDto getByIdExtends(Long id) {
        AttendanceSettingEntity attendanceSettingEntity = baseMapper.getByIdExtends(id);
        return ConvertUtil.transformObj(attendanceSettingEntity, AttendanceSettingDto.class);
    }

    @Override
    public List<AttendanceSettingDto> listAttendanceSetting(AttendanceSettingDto attendanceSettingDto) {
        QueryWrapper<AttendanceSettingEntity> wrapper = getWrapper(attendanceSettingDto);
        List<AttendanceSettingEntity> list = baseMapper.selectList(wrapper);
        return ConvertUtil.transformObjList(list, AttendanceSettingDto.class);
    }


    @Override
    public AttendanceSettingDto currentUserSetting(SysUser currentUser, Integer type) {
        HashMap<String, Object> param = new HashMap<>();
        param.put("id", currentUser.getId());
        //查询当前用户所在部门  以及所有部门的路径
        final Long deptId = currentUser.getDeptId();//部门ID  按照
        param.put("deptId", deptId);
        param.put("type", type);
        final SysDeptEntity dept = sysDeptService.getById(deptId);
        String parents = dept.getParents();
        if (parents != null && !"".equals(parents)) {
            final String[] split = parents.split(",");
            param.put("depts", Arrays.asList(split));
        }
        AttendanceSettingEntity attendanceSettingEntity = baseMapper.currentUserSetting(param);
        return ConvertUtil.transformObj(attendanceSettingEntity, AttendanceSettingDto.class);
    }
}

